package com.qau.zek.controller;

import com.qau.zek.pojo.shops;
import com.qau.zek.pojo.suppliers;
import com.qau.zek.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/suppliers")
public class SuppliersServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // JDBC
        System.out.println("supplierServlet.service()");
        //解决post请求中文乱码问题
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        switch (method) {
            case "add_suppliers":
                add_suppliers(request, response);
                break;
            case "select_suppliers":
                select_suppliers(request, response);
                break;
            case "deleteById":
                deleteById(request, response);
                break;
            case "selectById":
                selectById(request, response);
                break;
            case "update":
                update(request, response);
                break;
            default:
                break;
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("SuppliersServlet.update()");
        String supplier_id = request.getParameter("supplier_id");
        String supplier_name = request.getParameter("supplier_name");
        String supplier_loc = request.getParameter("supplier_loc");
//        String supplier_price = request.getParameter("supplier_price");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update suppliers set sup_name=?,sup_loc=? where sup_id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(3, supplier_id);
            statement.setString(1, supplier_name);
            statement.setString(2, supplier_loc);
//            statement.setString(3,supplier_price);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/suppliers?method=select_suppliers");
    }

    private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("supplierServlet.selectById()");
        String supplier_id = request.getParameter("supplier_id");
        System.out.println(supplier_id);
        List<suppliers> list = new ArrayList<suppliers>();
        try {
            Connection connection = null;
            PreparedStatement statement = null;
            connection = JDBCUtil.getConnection();
            String sql = "select * from suppliers where sup_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, Integer.parseInt(supplier_id));
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int supplier_id1 = resultSet.getInt("sup_id");
                String supplier_name = resultSet.getString("sup_name");
                String supplier_loc = resultSet.getString("sup_loc");
//                double supplier_price = resultSet.getDouble("supplier_price");
                suppliers supplier = new suppliers(supplier_id1, supplier_name, supplier_loc);
                request.setAttribute("supplier", supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (suppliers supplier : list) {
            System.out.println(supplier);
        }

        request.getRequestDispatcher("/suppliers_update.jsp").forward(request, response);


    }

    private void add_suppliers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("supplierServlet.add()");
        String supplier_id = request.getParameter("supplier_id");
        String supplier_name = request.getParameter("supplier_name");
        String supplier_loc = request.getParameter("supplier_loc");

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into suppliers(sup_id,sup_name,sup_loc) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, supplier_id);
            statement.setString(2, supplier_name);
            statement.setString(3, supplier_loc);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //删除完之后，重定向到查找所有界面
        response.sendRedirect(request.getContextPath() + "/suppliers?method=select_suppliers");
    }

    private void select_suppliers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<suppliers> list = new ArrayList<suppliers>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select sup_id,sup_name,sup_loc from suppliers";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                //每遍历一次查出一个User
                int supplier_id = resultSet.getInt("sup_id");
                String supplier_name = resultSet.getString("sup_name");
                String supplier_loc = resultSet.getString("sup_loc");


                suppliers supplier = new suppliers(supplier_id, supplier_name, supplier_loc);
                list.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (suppliers supplier : list) {
            System.out.println(supplier);
        }

        //把查找出来的数据放到req域对象里面（一块内存区域）
        request.setAttribute("supplier_list", list);
        //转发到user_list.jsp页面进行展示
        request.getRequestDispatcher("/suppliers_list.jsp").forward(request, response);
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("supplierServlet.deleteById()");
        String supplier_id = request.getParameter("supplier_id");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from suppliers where sup_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(supplier_id));
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.sendRedirect(request.getContextPath() + "/suppliers?method=select_suppliers");
    }


}
